
package com.benimetum.program;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author Ethem Cem Ozkan
 * @email ethemcem.ozkan@gmail.com
 * No 07110604
 */
public class ProgramParser
{
    public static String getProgram() throws MalformedURLException, IOException{
	  URL url = new URL("http://kayit.etu.edu.tr/Ders/_Ders_prg_start.php");
	  URLConnection conn = url.openConnection();
	  BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	  String line;
	  String result = "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
	  while ((line = rd.readLine()) != null) {
		if(!line.contains("<img")&&(!line.contains("<table"))&&(!line.contains("<td"))&&(!line.contains("</td>"))&&(!line.contains("<th"))&&(!line.contains("</th>"))&&(!line.contains("<tr"))&&(!line.contains("</tr>"))&&(!line.contains("</table")))
		result+=line;
	  }
	  result=result.replaceAll("<center>", "");
	  result=result.replaceAll("</center>", "");
	  result+="</html>";
	  return result;
    } 
    public static void parse(String input){
	  try {
		DocumentBuilderFactory dbf =
		    DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		InputSource is = new InputSource();
		is.setCharacterStream(new StringReader(input));
		Document doc = db.parse(is);
		NodeList nodes = doc.getElementsByTagName("th");
	  } catch (SAXException ex) {
		Logger.getLogger(ProgramParser.class.getName()).log(Level.SEVERE, null, ex);
	  } catch (IOException ex) {
		Logger.getLogger(ProgramParser.class.getName()).log(Level.SEVERE, null, ex);
	  } catch (ParserConfigurationException ex) {
		Logger.getLogger(ProgramParser.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }

    
}
